layui.use(['table', 'admin', 'laydate', 'ax', 'upload'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var upload = layui.upload;
    /**
     * 管理
     */
    var Hycy = {
        tableId: "hycyTable"
    };

    /**
     * 初始化表格的列
     */
    Hycy.initColumn = function () {
        return [[
            {field: 'mc', sort: true, title: ''},
            {field: 'hyxj', sort: true, title: '现金'},
            {field: 'hyzfb', sort: true, title: '支付宝'},
            {field: 'hywx', sort: true, title: '微信'},
            {field: 'hymd', sort: true, title: '点评买单'},
            {field: 'rq', sort: true, title: '日期'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    Hycy.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(Hycy.tableId, {where: queryData});
    };

    /**
     * 弹出添加对话框
     */
    Hycy.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/hycy/add';
    };

    /**
     * 导出excel按钮
     */
    Hycy.exportExcel = function () {
        var checkRows = table.checkStatus(Hycy.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Hycy.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hycy/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Hycy.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hycy/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Hycy.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Hycy.tableId,
        url: Feng.ctxPath + '/hycy/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Hycy.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Hycy.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Hycy.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Hycy.exportExcel();
    });
    //渲染时间选择框
    laydate.render({
        elem: "#dataTime",
        type: 'date',
        tiger: 'click'
    });

    /**
     * 导入Excel
     */
    var loadIndex;
    upload.render({
        elem: '#btnExcel'
        , url: '/hycy/import'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            this.data = {'dataTime': $('#dataTime').val()};//关键代码
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
        }
        , done: function (res) {
            if (res == 1) {
                Feng.success("导入成功!");
                window.location.reload();
                layer.close(loadIndex);
            } else if (res == 3) {
                Feng.error("请选择日期!");
            } else {
                Feng.success("导入失败!");
            }

        }
    });


    // 工具条点击事件
    table.on('tool(' + Hycy.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Hycy.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Hycy.onDeleteItem(data);
        }
    });
});
